<%layout("/eova/layout/default.html",{'title' : '查看公文'}){%>

<body>
	<script type="text/javascript"
		src="webapp/plugins/sursen/CallXMLHttpRequest.js">
		
	</script>

	<script type="text/javascript">
		var S_DecodeAddress = "172.16.1.55/sursen";
		var S_ReadServletUrl = "/ReadFile?method=";
		
		//阅文操作
		function lks_ReadGwFile(serverFilePath, serverFileName, newWin) {
			
			if (newWin == null)
				winOpenType = "";
			else
				winOpenType = newWin;
			
			if (!downGwDoc(serverFilePath, serverFileName)) {
				alert("本地文件创建失败请检查！");
				return;
			}
			readGwDoc(serverFileName);
		}

		function getServlet(url, sMethod) {
			return S_DecodeAddress + url + sMethod;
		}

		function uploadserver(fileName, httpurl) {
			if (fileName == "")
				return false;
			if (!IsPathExist(fileName)) {
				alert("文件不存在（" + fileName + "）请检查！");
				return false;
			}
			var tempFileName = getCodeFileName(fileName);
			delFile(fileName);
			retValue = SedFun.UpLoadFileEx(tempFileName, httpurl);
			delFile(tempFileName);
			return retValue;
		}
		function getCodeFileName(sFile) {
			try {
				var sFileTemp = getTmpFileName(getStx(sFile));
				var fso = new ActiveXObject("Scripting.FileSystemObject");
				var orgf = fso.GetFile(sFile);
				orgf.Copy(sFileTemp);
			} catch (err) {
				return sFile;
			}
			return sFileTemp;
		}

		function downGwDoc(serverFilePath, serverFileName) {
			if (!initCurAct())
				return;
			WebOffice.WebUrl = Com_GetCurDnsHost()
					+ "${KMSS_Parameter_ContextPath}km/imissive/sursen/sursen_service.jsp";
			WebOffice.WebSetMsgByName("svcType", "get");
			WebOffice.WebSetMsgByName("sursenDocFilePath", serverFilePath);
			var localFileName = tempPath + "\\" + serverFileName;

			var ret = false;
			//alert(localFileName);
			//alert(serverFilePath);
			//alert(serverFileName);
			if (WebOffice.WebGetFile(localFileName, serverFileName))
				ret = true;
			WebOffice.WebClearMessage();
			return ret;
		}

		//在线脱密阅读gw文件
		function readGwDoc(docname) {
			if (docname == "")
				return;
			docname = getFullFileName(docname);
			if (!IsPathExist(docname)) {
				alert("待阅读文件不存在（" + docname + "）请检查！");
				return;
			}
			sFileName = docname;
			var surl = getServlet(S_ReadServletUrl, "key");
			callserver(surl, GetReadKey);

			return;
		}

		function GetReadKey(status, statusText, responseText, responseXML) {
			var retsealname = "false";
			if (status == 200) {
				var sn = responseText;
				readGw(sn);
			} else {
				alert("与书生加解密服务器通讯失败！");
			}
		}

		function readGw(keyName) {
			var srcfile = keyName + ".gw";
			var mbfile = keyName + ".gd";
			tFileName = mbfile;
			var uploadurl = getServlet(S_ReadServletUrl,
					"save&smethod=guidang&srcfile=" + srcfile + "&mbfile="
							+ mbfile + "&random="
							+ Math.round(Math.random() * 100));
			var flag = uploadserver(sFileName, uploadurl);
			if (flag) {
				uploadurl = getServlet(S_ReadServletUrl, "guidang&mbfile="
						+ mbfile + "&random=" + Math.round(Math.random() * 100));
				callserver(uploadurl, readGwBack);
			} else {
				alert("与加密服务器通讯失败!服务器地址:" + uploadurl);
			}
		}

		function readGwBack(status, statusText, responseText, responseXML) {
			if (status == 200) {
				var sn = responseText.split("|");
				if (sn[0] != "-1") {
					var readurl = getServlet(S_ReadServletUrl, "download");
					readurl = readurl + "&filename=" + tFileName;
					delFile(sFileName);
					displayGw(readurl);
				} else {
					alert("归档失败,原因是：" + sn[1]);
				}
			} else {
				alert("归档失败：" + statusText);
			}
		}
		function displayGw(readurl) {
			//alert(readurl);
			try {
				if (winOpenType == "new") {
					window.open(readurl);
				} else {
					var gwiframe = $("#gwiframe");
					if (gwiframe == null)
						return;
					gwiframe.attr("src", readurl);
				}
				sFileName = "";
				tFileName = "";
				return;
			} catch (e) {
				alert("下载文件失败，请联系管理员");
			}
		}
	</script>

	<script>
		$(function() {
			lks_ReadGwFile('${sursenDocFilePath}', '${sursenDocFileName}');
		});
	</script>

</body>

<%}%>
